package com.mmy.controller.sys;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.sys.TSysAddress;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.sys.SysAddressServiceImpl;
import com.mmy.service.impl.sys.SysUserServiceImpl;

@Controller
public class SysAddressController {
	@Autowired
	SysUserServiceImpl sysUserServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	SysAddressServiceImpl sysAddressServiceImpl;

	/**
	 * 函数功能说明：前台个人中心进入创建地址页面
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/tocreatesysaddress")
	public String toCreateSysAddress(Model model) {
		// 查询所有区域省市区
		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		List<TConstZone> lstZoneCity = constZoneServiceImpl.findAllCity();
		model.addAttribute("lstZoneCity", lstZoneCity);
		List<TConstZone> lstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		model.addAttribute("lstZoneDistrict", lstZoneDistrict);
		return "/permanagement/CreateAddress";
	}

	/**
	 * 函数功能说明：前台个人中心提交创建的地址
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/savecreatesysaddress")
	public String saveCreateSysAddress(TSysAddress tSysAddtress, HttpSession session) throws Exception {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		sysAddressServiceImpl.createAddress(tSysAddtress, sessionSysUser);
		return "redirect:perinfo";

	}

	/**
	 * 函数功能说明：前台个人中心进入修改地址页面
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping("/toupdatesysaddress")
	public String toUpdateSysAddressCheckedFind(HttpServletRequest request, Model model) {
		String iAddressID = request.getParameter("iAddressID");
		TSysAddress dbSysAddress = sysAddressServiceImpl.findByAddressID(Integer.parseInt(iAddressID));
		model.addAttribute("dbSysAddress", dbSysAddress);
		// 查询所有省市区
		List<TConstZone> lstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("lstZoneProvince", lstZoneProvince);
		List<TConstZone> lstZoneCity = constZoneServiceImpl.findAllCity();
		model.addAttribute("lstZoneCity", lstZoneCity);
		List<TConstZone> lstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		model.addAttribute("lstZoneDistrict", lstZoneDistrict);

		return "/permanagement/UpdateAddress";
	}

	/**
	 * 函数功能说明：前台个人中心提交修改的收货信息
	 *
	 * @param 参数说明 tSysUser:用户类 用户 session
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.10.29
	 */
	@RequestMapping(value = "/saveupdatesysaddress")
	public String saveUpdateSysAddress(TSysAddress tSysAddress, HttpSession session) throws Exception {
		TSysUser sessionSysUser = (TSysUser) session.getAttribute("tSysUser");
		sysAddressServiceImpl.updateSysAddress(sessionSysUser, tSysAddress);
		return "redirect:perinfo";

	}

	/**
	 * 函数功能说明： 删除收货信息信息
	 *
	 * @param 参数说明 tSysAddress
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2020.08.18
	 */
	@PostMapping("/deletesysaddresschecked")
	@ResponseBody
	public String deleteSysAddressChecked(String checkList) {
		sysAddressServiceImpl.deleteChecked(checkList);
		return "redirect:perinfo";

	}
}
